package com.njtech.pet.dao.example;


import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.BillDao;
import com.njtech.pet.entity.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;


/**
 * @author lxj
 * Date 2020-03-19 10:35
 * Description BillDao测试类
 */

public class BillDaoExample {
    static public ApplicationContext context;
    static public BillDao billDao;
    @Before
    public void prepare() {
        context = new ClassPathXmlApplicationContext("spring-dao.xml");
        billDao = (BillDao) context.getBean("billDao");
        System.out.println("prepared");
    }

    @Test
    public void testInsertBill() {
        User user = new User();
        user.setId(1);
        Item item = new Item();
        item.setId(1);
        Person buyer = new Person();
        Person insured = new Person();
        buyer.setId(1);
        insured.setId(1);
        Pet pet = new Pet();
        pet.setId(1);
        Bill bill = new Bill(null, user, item, null, Constant.Bill.STATE_BILL_UNPAID.getValue(), Constant.Bill.TYPE_COUPON.getValue(), buyer, insured, pet, null, null, null, 500.0, 600, null, null, "extra5"
        );
        int row = billDao.insert(bill);
        System.out.println("insert result : " + row);
    }

    @Test
    public void testUpdateBill() {

        Item item = new Item();
        item.setId(6);
        Person buyer = new Person();
        Person insured = new Person();
        buyer.setId(2);
        insured.setId(1);
        Pet pet = new Pet();
        pet.setId(6);
        Bill bill = new Bill(1, null, item, null, Constant.Bill.STATE_BILL_PAID.getValue(), Constant.Bill.TYPE_INSURANCE.getValue(), buyer, insured, pet, null, null, null, 500.0, 600, null, null, null
        );
        int row = billDao.updateByBillId(bill);
        System.out.println("update result : " + row);
    }

    @Test
    public void testSelectById() {
        int id = 1;
        System.out.println(billDao.selectByBillId(id));
    }

    @Test
    public void testSelectGroupBills() {
        User user = new User();
        user.setId(3);
        List<Bill> bills = billDao.selectByUser(user, Constant.Bill.STATE_BILL_UNPAID.getValue(), Constant.Bill.TYPE_INSURANCE.getValue(), "2020-03-19", "2020-03-19");
        for (Bill bill : bills) {
            System.out.println(bill);
        }
    }
}
